/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ik.RestauranteBar.SMO.Ventanas;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import org.ik.RestauranteBar.SMO.Encapsulados.DetallesPedidos;
import org.ik.RestauranteBar.SMO.Encapsulados.DetallesPedidosPK;
import org.ik.RestauranteBar.SMO.Ventanas.Modelos.TableModelDirecciones;
import org.ik.RestauranteBar.SMO.Encapsulados.Direcciones;
import org.ik.RestauranteBar.SMO.Encapsulados.DireccionesPK;
import org.ik.RestauranteBar.SMO.Encapsulados.Tablas.Direcciones_Tabla;
import org.ik.RestauranteBar.SMO.Encapsulados.Estadospedido;
import org.ik.RestauranteBar.SMO.Encapsulados.Tipoproducto;
import org.ik.RestauranteBar.SMO.Encapsulados.Usuarios;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorClientes;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorDirecciones;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorPedidos;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorProductos;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorTipoProducto;
import org.ik.RestauranteBar.SMO.Encapsulados.Productos;
import org.ik.RestauranteBar.SMO.Encapsulados.Tablas.DetallesPedidos_Tabla;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorDetallesPedidos;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorEstadosPedido;
import org.ik.RestauranteBar.SMO.Ventanas.Modelos.TableModelDetalles;

/**
 *
 * @author Malkavian
 */
public class Pedidos extends javax.swing.JFrame implements ActionListener {

    Usuarios u = new Usuarios();
    org.ik.RestauranteBar.SMO.Encapsulados.Clientes cliente = new org.ik.RestauranteBar.SMO.Encapsulados.Clientes();
    org.ik.RestauranteBar.SMO.Encapsulados.Direcciones direccion = new Direcciones();
    org.ik.RestauranteBar.SMO.Encapsulados.DireccionesPK direccion_pk = new DireccionesPK();
    List<Direcciones_Tabla> direcciones_tabla = new ArrayList<Direcciones_Tabla>();
    List<Direcciones> direcciones = new ArrayList<Direcciones>();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    public Pedidos(Usuarios u) {

        this.u = u;

        List<Tipoproducto> tipo = ManejadorProductos.getInstance().getTiposUtilizados();
        JPanel panelProductos = new JPanel();
        panelProductos.setSize(750, 680);

        panelProductos.setLayout(new GridLayout(tipo.size(), 1));

        for (Tipoproducto t : tipo) {

            List<Productos> prod = ManejadorProductos.getInstance().getProductoPorTipo(t.getId());

            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 4, 8, 8));

            for (Productos p : prod) {

                JButton boton = new JButton(p.getIdproducto() + "-" + p.getNombre());

                boton.addActionListener(this);

                panel.add(boton);

            }

            panel.setBorder(new TitledBorder(t.getDescripcion()));
            panelProductos.add(panel);
        }
        JScrollPane scroll = new JScrollPane(panelProductos);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        initComponents();

        panelFinal.setLayout(new FlowLayout());
        panelFinal.setSize(751, 681);
        panelFinal.add(scroll);

        tabla_detalles.setModel(new TableModelDetalles());

        //llena fecha
        texto_fecha_pedido.setText(sdf.format(new Date()));
        texto_no_pedido.setText("" + (ManejadorPedidos.getInstance().maxPedidos() + 1));

        //llena estados
        List<Estadospedido> estados = ManejadorEstadosPedido.getInstance().getEstadospedido();

        comboEstados.removeAllItems();
        comboEstados.addItem("Seleccionar");

        for (Estadospedido e : estados) {
            if (e.getActivo()) {
                comboEstados.addItem(e.getDescripcion());
            }
        }

        comboMesas.removeAllItems();
        comboMesas.addItem("Seleccionar");

        for (int i = 1; i <= 20; i++) {

            comboMesas.addItem("" + i);

        }

        Dimension d = new Dimension(this.getSize());
        Dimension g = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(g.width / 2 - d.width / 2, g.height / 2 - d.height / 2, d.width, d.height);

        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        jSpinner1 = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        texto_telefono = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        texto_nombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        texto_apellido = new javax.swing.JTextField();
        boton_busqueda_cliente = new javax.swing.JButton();
        esCliente = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_detalles = new javax.swing.JTable();
        boton_limpiar_detalle = new javax.swing.JButton();
        boton_borrar_detalle = new javax.swing.JButton();
        texto_total = new javax.swing.JTextField();
        texto_total_itbis = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        texto_total_totales = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        boton_guardar_pedido = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        boton_facturar_pedido = new javax.swing.JButton();
        boton_limpiar_pedido = new javax.swing.JButton();
        boton_anular_pedido = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        texto_no_pedido = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        texto_fecha_pedido = new javax.swing.JTextField();
        boton_busqueda_pedido = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        textarea_comentario = new javax.swing.JTextArea();
        jLabel11 = new javax.swing.JLabel();
        texto_direccion = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        boton_busqueda_pedido1 = new javax.swing.JButton();
        comboEstados = new javax.swing.JComboBox();
        jLabel14 = new javax.swing.JLabel();
        comboMesas = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        panelFinal = new javax.swing.JPanel();

        jTextField1.setText("jTextField1");

        jTextField4.setText("jTextField4");

        jTextField2.setText("jTextField2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("SMP - Información Pedidos");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Información Cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Teléfono:");

        texto_telefono.setEditable(false);
        try {
            texto_telefono.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###-###-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        texto_telefono.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_telefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                texto_telefonoKeyTyped(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Nombre:");

        texto_nombre.setEditable(false);
        texto_nombre.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Apellido:");

        texto_apellido.setEditable(false);
        texto_apellido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        boton_busqueda_cliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/RestauranteBar/SMO/Recursos/group.png"))); // NOI18N
        boton_busqueda_cliente.setEnabled(false);

        esCliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        esCliente.setText("Cliente");
        esCliente.setEnabled(false);
        esCliente.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                esClienteStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(texto_apellido))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(esCliente)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(11, 11, 11)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(texto_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(boton_busqueda_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(texto_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(boton_busqueda_cliente)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(texto_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(esCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(texto_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(texto_apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Detalles Pedido", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        tabla_detalles.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabla_detalles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabla_detallesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabla_detalles);

        boton_limpiar_detalle.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_limpiar_detalle.setText("Limpiar");
        boton_limpiar_detalle.setEnabled(false);
        boton_limpiar_detalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_limpiar_detalleActionPerformed(evt);
            }
        });

        boton_borrar_detalle.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_borrar_detalle.setText("Borrar Detalle");
        boton_borrar_detalle.setEnabled(false);
        boton_borrar_detalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_borrar_detalleActionPerformed(evt);
            }
        });

        texto_total.setEditable(false);
        texto_total.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_total.setText("0");

        texto_total_itbis.setEditable(false);
        texto_total_itbis.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_total_itbis.setText("0");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Totales:");

        texto_total_totales.setEditable(false);
        texto_total_totales.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_total_totales.setText("0");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Monto Total:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(boton_limpiar_detalle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton_borrar_detalle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(texto_total_itbis, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(texto_total, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(texto_total_totales, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(texto_total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(texto_total_itbis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(boton_limpiar_detalle)
                    .addComponent(boton_borrar_detalle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(texto_total_totales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        boton_guardar_pedido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_guardar_pedido.setText("Guardar Pedido");
        boton_guardar_pedido.setEnabled(false);
        boton_guardar_pedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_guardar_pedidoActionPerformed(evt);
            }
        });

        jButton6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton6.setText("Cerrar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        boton_facturar_pedido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_facturar_pedido.setText("Facturar Pedido");
        boton_facturar_pedido.setEnabled(false);
        boton_facturar_pedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_facturar_pedidoActionPerformed(evt);
            }
        });

        boton_limpiar_pedido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_limpiar_pedido.setText("Limpiar Pantalla");
        boton_limpiar_pedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_limpiar_pedidoActionPerformed(evt);
            }
        });

        boton_anular_pedido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_anular_pedido.setText("Anular Pedido");
        boton_anular_pedido.setEnabled(false);
        boton_anular_pedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_anular_pedidoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(boton_guardar_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boton_anular_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(boton_facturar_pedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(boton_limpiar_pedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(boton_guardar_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boton_facturar_pedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(boton_anular_pedido, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                    .addComponent(boton_limpiar_pedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Información Pedidos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("No. Pedido:");

        texto_no_pedido.setEditable(false);
        texto_no_pedido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("Fecha:");

        texto_fecha_pedido.setEditable(false);
        texto_fecha_pedido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        boton_busqueda_pedido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/RestauranteBar/SMO/Recursos/find.png"))); // NOI18N
        boton_busqueda_pedido.setEnabled(false);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setText("Comentaio:");

        textarea_comentario.setColumns(20);
        textarea_comentario.setRows(5);
        jScrollPane2.setViewportView(textarea_comentario);

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel11.setText("Estado:");

        texto_direccion.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel13.setText("Dirección:");

        boton_busqueda_pedido1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/RestauranteBar/SMO/Recursos/find.png"))); // NOI18N
        boton_busqueda_pedido1.setEnabled(false);

        comboEstados.setEditable(true);
        comboEstados.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        comboEstados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboEstados.setEnabled(false);

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel14.setText("Mesa:");

        comboMesas.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        comboMesas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                        .addComponent(boton_busqueda_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(texto_no_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                    .addComponent(jLabel11)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(jPanel4Layout.createSequentialGroup()
                                    .addComponent(jLabel14)
                                    .addGap(16, 16, 16)))
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(10, 10, 10)))
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(texto_fecha_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(comboMesas, 0, 148, Short.MAX_VALUE)
                                .addComponent(comboEstados, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(boton_busqueda_pedido1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel13)
                                .addGap(4, 4, 4))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(18, 18, 18)))
                        .addComponent(jScrollPane2))
                    .addComponent(texto_direccion))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(boton_busqueda_pedido, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(texto_no_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel10)))
                        .addGap(8, 8, 8)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(texto_fecha_pedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(comboEstados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel11))
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel13))
                                .addComponent(boton_busqueda_pedido1)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboMesas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel14)
                            .addComponent(texto_direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/RestauranteBar/SMO/Recursos/posted.png"))); // NOI18N

        panelFinal.setOpaque(false);

        javax.swing.GroupLayout panelFinalLayout = new javax.swing.GroupLayout(panelFinal);
        panelFinal.setLayout(panelFinalLayout);
        panelFinalLayout.setHorizontalGroup(
            panelFinalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 722, Short.MAX_VALUE)
        );
        panelFinalLayout.setVerticalGroup(
            panelFinalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46)
                        .addComponent(jLabel12))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelFinal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelFinal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        Principal p = new Principal(u);
        dispose();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void texto_telefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_texto_telefonoKeyTyped
        if (evt.getKeyChar() == '\n') {

            if (texto_telefono.getText().toCharArray().length == 12) {

                cliente = ManejadorClientes.getInstance().getClientePorTelefono(texto_telefono.getText());

                if (cliente != null) {

                    texto_nombre.setText(cliente.getNombre().trim());
                    texto_apellido.setText(cliente.getApellido().trim());

//                    direcciones = ManejadorDirecciones.getInstance().getDireccionesCliente(texto_telefono.getText());
//
//                    if (direcciones_tabla != null) {
//                        for (int i = 0; i < direcciones_tabla.size(); i++) {
//                            direcciones_tabla.remove(i);
//                        }
//                    }
//
//                    for (Direcciones d : direcciones) {
//                        Direcciones_Tabla dt = new Direcciones_Tabla();
//
//                        dt.setTelefono(d.getDireccionesPK().getClientesTelefono());
//                        dt.setIddireccion(d.getDireccionesPK().getIddireccion());
//                        dt.setDireccion(d.getDireccion());
//                        direcciones_tabla.add(dt);
//
//                    }

//                    tabla_detalles.setModel(new TableModelDirecciones(direcciones_tabla));

                    texto_apellido.setEnabled(true);
                    texto_nombre.setEnabled(true);
                    //   boton_guardar_pedido.setEnabled(true);
                    boton_limpiar_detalle.setEnabled(true);
                    boton_borrar_detalle.setEnabled(true);
                    boton_busqueda_pedido.setEnabled(true);
                    tabla_detalles.setEnabled(true);
                    texto_no_pedido.setEditable(true);
                    textarea_comentario.setEditable(true);

                    int max = ManejadorPedidos.getInstance().maxPedidosPorCliente(cliente.getTelefono());

                    if (max == 0) {
                        texto_no_pedido.setText("1");
                        texto_no_pedido.setEditable(false);
                        boton_busqueda_pedido.setEnabled(false);

                    } else {
                        max++;
                        texto_no_pedido.setText("" + max);
                    }


                } else {
                    JOptionPane.showMessageDialog(null, "TeléfonJOptiono no existe.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Teléfono no existe.", "Error", JOptionPane.ERROR_MESSAGE);
            }

        }
    }//GEN-LAST:event_texto_telefonoKeyTyped

    private void boton_guardar_pedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_guardar_pedidoActionPerformed

        if (!(comboMesas.getSelectedIndex() == 0)) {

            org.ik.RestauranteBar.SMO.Encapsulados.Pedidos p = new org.ik.RestauranteBar.SMO.Encapsulados.Pedidos();

            p.setIdpedido(Integer.parseInt(texto_no_pedido.getText()));

            if (!esCliente.isSelected()) {
                p.setClientesTelefono(ManejadorClientes.getInstance().getClientePorTelefono("000-000-0000"));
            } else {
                p.setClientesTelefono(ManejadorClientes.getInstance().getClientePorTelefono(texto_telefono.getText()));
            }

            p.setTotalPagado(new BigDecimal("0"));
            p.setTotalPedido(new BigDecimal(texto_total_totales.getText()));
            p.setMesa(comboMesas.getSelectedIndex());
            p.setComentario(textarea_comentario.getText());
            p.setEstadosPedidoid(ManejadorEstadosPedido.getInstance().getEstadospedido(1));
            p.setUsuariosUsuario(u);
            p.setDireccionesClientesTelefono(texto_direccion.getText());
            p.setFechaPedido(new Date());

            ManejadorPedidos.getInstance().insertarPedidos(p);

            TableModelDetalles tabla = (TableModelDetalles) tabla_detalles.getModel();

            for (int i = 0; i < tabla.getRowCount(); i++) {

                DetallesPedidos_Tabla ta = (DetallesPedidos_Tabla) tabla.getRow(i);

                DetallesPedidosPK detallePK = new DetallesPedidosPK();

                detallePK.setPedidosIdpedido(p.getIdpedido());
                detallePK.setIddetalle((i + 1));

                DetallesPedidos detalle = new DetallesPedidos(detallePK);

                detalle.setProductosidproducto(ManejadorProductos.getInstance().getProducto(ta.getNo()));
                detalle.setCantidad(ta.getCantidad());
                detalle.setPrecio(ta.getPrecio());
                detalle.setImpuestos(ta.getImpuestos());
                detalle.setTotaldetalle(detalle.getPrecio().add(detalle.getImpuestos()));

                ManejadorDetallesPedidos.getInstance().insertarDetallesPedidos(detalle);

                boton_limpiar_pedido.doClick();
            }

        } else {
            JOptionPane.showMessageDialog(null, "Debe elegir una Mesa", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_boton_guardar_pedidoActionPerformed

    private void boton_limpiar_detalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_limpiar_detalleActionPerformed
        tabla_detalles.setModel(new TableModelDetalles());
        texto_total.setText("0");
        texto_total_itbis.setText("0");
        texto_total_totales.setText("0");
        boton_borrar_detalle.setEnabled(false);
        boton_limpiar_detalle.setEnabled(false);
        boton_facturar_pedido.setEnabled(false);
        boton_anular_pedido.setEnabled(false);
        boton_guardar_pedido.setEnabled(false);
    }//GEN-LAST:event_boton_limpiar_detalleActionPerformed

    private void tabla_detallesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabla_detallesMouseClicked
        evt.getSource();
    }//GEN-LAST:event_tabla_detallesMouseClicked

    private void esClienteStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_esClienteStateChanged
        if (esCliente.isSelected()) {
            boton_busqueda_cliente.setEnabled(true);
            texto_telefono.setEnabled(true);
        } else {
            boton_busqueda_cliente.setEnabled(false);
            texto_telefono.setEnabled(false);
        }
    }//GEN-LAST:event_esClienteStateChanged

    private void boton_facturar_pedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_facturar_pedidoActionPerformed

        if (!(comboMesas.getSelectedIndex() == 0)) {

            org.ik.RestauranteBar.SMO.Encapsulados.Pedidos p = new org.ik.RestauranteBar.SMO.Encapsulados.Pedidos();

            p.setIdpedido(Integer.parseInt(texto_no_pedido.getText()));

            if (!esCliente.isSelected()) {
                p.setClientesTelefono(ManejadorClientes.getInstance().getClientePorTelefono("000-000-0000"));
            } else {
                p.setClientesTelefono(ManejadorClientes.getInstance().getClientePorTelefono(texto_telefono.getText()));
            }

            p.setTotalPagado(new BigDecimal(texto_total_totales.getText()));
            p.setTotalPedido(new BigDecimal(texto_total_totales.getText()));
            p.setMesa(comboMesas.getSelectedIndex());
            p.setComentario(textarea_comentario.getText());
            p.setEstadosPedidoid(ManejadorEstadosPedido.getInstance().getEstadospedido(2));
            p.setUsuariosUsuario(u);
            p.setDireccionesClientesTelefono(texto_direccion.getText());
            p.setFechaPedido(new Date());

            ManejadorPedidos.getInstance().insertarPedidos(p);

            TableModelDetalles tabla = (TableModelDetalles) tabla_detalles.getModel();

            for (int i = 0; i < tabla.getRowCount(); i++) {

                DetallesPedidos_Tabla ta = (DetallesPedidos_Tabla) tabla.getRow(i);

                DetallesPedidosPK detallePK = new DetallesPedidosPK();

                detallePK.setPedidosIdpedido(p.getIdpedido());
                detallePK.setIddetalle((i + 1));

                DetallesPedidos detalle = new DetallesPedidos(detallePK);

                detalle.setProductosidproducto(ManejadorProductos.getInstance().getProducto(ta.getNo()));
                detalle.setCantidad(ta.getCantidad());
                detalle.setPrecio(ta.getPrecio());
                detalle.setImpuestos(ta.getImpuestos());
                detalle.setTotaldetalle(detalle.getPrecio().add(detalle.getImpuestos()));

                ManejadorDetallesPedidos.getInstance().insertarDetallesPedidos(detalle);

                boton_limpiar_pedido.doClick();
            }

        } else {
            JOptionPane.showMessageDialog(null, "Debe elegir una Mesa", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_boton_facturar_pedidoActionPerformed

    private void boton_limpiar_pedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_limpiar_pedidoActionPerformed

        boton_borrar_detalle.setEnabled(false);
        boton_limpiar_detalle.setEnabled(false);
        boton_facturar_pedido.setEnabled(false);
        boton_anular_pedido.setEnabled(false);
        boton_guardar_pedido.setEnabled(false);

        tabla_detalles.setModel(new TableModelDetalles());
        texto_total.setText("0");
        texto_total_itbis.setText("0");
        texto_total_totales.setText("0");
        textarea_comentario.setText("");
        texto_direccion.setText("");

        //llena fecha
        texto_fecha_pedido.setText(sdf.format(new Date()));
        texto_no_pedido.setText("" + (ManejadorPedidos.getInstance().maxPedidos() + 1));

        //llena estados
        List<Estadospedido> estados = ManejadorEstadosPedido.getInstance().getEstadospedido();

        comboEstados.removeAllItems();
        comboEstados.addItem("Seleccionar");

        for (Estadospedido e : estados) {
            if (e.getActivo()) {
                comboEstados.addItem(e.getDescripcion());
            }
        }

        comboMesas.removeAllItems();
        comboMesas.addItem("Seleccionar");

        for (int i = 1; i <= 20; i++) {

            comboMesas.addItem("" + i);

        }
    }//GEN-LAST:event_boton_limpiar_pedidoActionPerformed

    private void boton_anular_pedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_anular_pedidoActionPerformed

        org.ik.RestauranteBar.SMO.Encapsulados.Pedidos p = ManejadorPedidos.getInstance().getPedido(Integer.parseInt(texto_no_pedido.getText()));

        if (p != null) {
            p.setEstadosPedidoid(ManejadorEstadosPedido.getInstance().getEstadospedido(3));
            ManejadorPedidos.getInstance().insertarPedidos(p);
        } else {
            JOptionPane.showMessageDialog(null, "Pedido no Existe.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_boton_anular_pedidoActionPerformed

    private void boton_borrar_detalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_borrar_detalleActionPerformed

        if (tabla_detalles.getSelectedRow() >= 0) {
            TableModelDetalles table_detalles = (TableModelDetalles) tabla_detalles.getModel();

            DetallesPedidos_Tabla detalleSeleccinado = (DetallesPedidos_Tabla) table_detalles.getRow(tabla_detalles.getSelectedRow());

            List<DetallesPedidos_Tabla> detalles = new ArrayList<DetallesPedidos_Tabla>();

            for (int i = 0; i < table_detalles.getRowCount(); i++) {

                DetallesPedidos_Tabla t = (DetallesPedidos_Tabla) table_detalles.getRow(i);

                if (t.getNo() != detalleSeleccinado.getNo()) {
                    detalles.add((DetallesPedidos_Tabla) table_detalles.getRow(i));
                }

            }

            tabla_detalles.setModel(new TableModelDetalles(detalles));

            BigDecimal impuestos = new BigDecimal(texto_total_itbis.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            BigDecimal total = new BigDecimal(texto_total.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            BigDecimal totalPago = new BigDecimal(texto_total_totales.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);

            impuestos = impuestos.subtract(detalleSeleccinado.getImpuestos());
            total = total.subtract(detalleSeleccinado.getPrecio());
            totalPago = impuestos.add(total);

            texto_total_itbis.setText("" + impuestos.doubleValue());
            texto_total.setText("" + total.doubleValue());
            texto_total_totales.setText("" + totalPago.doubleValue());

        }

        if (tabla_detalles.getRowCount() == 0) {

            boton_borrar_detalle.setEnabled(false);
            boton_limpiar_detalle.setEnabled(false);
            boton_facturar_pedido.setEnabled(false);
            boton_guardar_pedido.setEnabled(false);

        }

    }//GEN-LAST:event_boton_borrar_detalleActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_anular_pedido;
    private javax.swing.JButton boton_borrar_detalle;
    private javax.swing.JButton boton_busqueda_cliente;
    private javax.swing.JButton boton_busqueda_pedido;
    private javax.swing.JButton boton_busqueda_pedido1;
    private javax.swing.JButton boton_facturar_pedido;
    private javax.swing.JButton boton_guardar_pedido;
    private javax.swing.JButton boton_limpiar_detalle;
    private javax.swing.JButton boton_limpiar_pedido;
    private javax.swing.JComboBox comboEstados;
    private javax.swing.JComboBox comboMesas;
    private javax.swing.JCheckBox esCliente;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JPanel panelFinal;
    private javax.swing.JTable tabla_detalles;
    private javax.swing.JTextArea textarea_comentario;
    private javax.swing.JTextField texto_apellido;
    private javax.swing.JTextField texto_direccion;
    private javax.swing.JTextField texto_fecha_pedido;
    private javax.swing.JTextField texto_no_pedido;
    private javax.swing.JTextField texto_nombre;
    private javax.swing.JFormattedTextField texto_telefono;
    private javax.swing.JTextField texto_total;
    private javax.swing.JTextField texto_total_itbis;
    private javax.swing.JTextField texto_total_totales;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton o = (JButton) e.getSource();

        String[] data = o.getText().split("-");
        //verifica si existe para modificarlo
        TableModelDetalles tabla = (TableModelDetalles) tabla_detalles.getModel();
        DetallesPedidos_Tabla t = new DetallesPedidos_Tabla();
        boolean ind = true;

        for (int i = 0; i < tabla.getRowCount(); i++) {
            DetallesPedidos_Tabla a = (DetallesPedidos_Tabla) tabla.getRow(i);
            if (a.getNo() == Integer.parseInt(data[0])) {
                t = a;
                ind = false;
            }
        }

        //si no existe, lo agrega, en caso contrario le suma 1 a la cantidad
        if (ind) {

            Productos prod = ManejadorProductos.getInstance().getProducto(Integer.parseInt(data[0]));

            DetallesPedidos_Tabla detalle = new DetallesPedidos_Tabla();

            detalle.setNo(prod.getIdproducto());
            detalle.setNombre(prod.getNombre());

            if (prod.getLlevaImpuestos()) {
                BigDecimal impuesto = new BigDecimal("0");
                impuesto = prod.getPrecio().multiply(new BigDecimal("0.28"));
                detalle.setImpuestos(impuesto);
            } else {
                detalle.setImpuestos(new BigDecimal("0"));
            }
            detalle.setCantidad(1);
            detalle.setPrecio(prod.getPrecio());

            List<DetallesPedidos_Tabla> detalles = new ArrayList<DetallesPedidos_Tabla>();

            for (int i = 0; i < tabla.getRowCount(); i++) {
                detalles.add((DetallesPedidos_Tabla) tabla.getRow(i));
            }

            detalles.add(detalle);

            tabla_detalles.setModel(new TableModelDetalles(detalles));

            BigDecimal impuestos = new BigDecimal(texto_total_itbis.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            BigDecimal total = new BigDecimal(texto_total.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            BigDecimal totalPago = new BigDecimal(texto_total_totales.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);

            impuestos = impuestos.add(detalle.getImpuestos());
            total = total.add(detalle.getPrecio());
            totalPago = impuestos.add(total);

            texto_total_itbis.setText("" + impuestos.doubleValue());
            texto_total.setText("" + total.doubleValue());
            texto_total_totales.setText("" + totalPago.doubleValue());

        } else {

            List<DetallesPedidos_Tabla> detalles = new ArrayList<DetallesPedidos_Tabla>();

            for (int i = 0; i < tabla.getRowCount(); i++) {

                DetallesPedidos_Tabla ta = (DetallesPedidos_Tabla) tabla.getRow(i);

                if (ta.getNo() == t.getNo()) {
                    ta.setCantidad((ta.getCantidad() + 1));
                }

                detalles.add(ta);
            }


            tabla_detalles.setModel(new TableModelDetalles(detalles));

            BigDecimal impuestos = new BigDecimal(texto_total_itbis.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            BigDecimal total = new BigDecimal(texto_total.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            BigDecimal totalPago = new BigDecimal(texto_total_totales.getText()).setScale(2, BigDecimal.ROUND_HALF_EVEN);

            impuestos = impuestos.add(t.getImpuestos());
            total = total.add(t.getPrecio());
            totalPago = impuestos.add(total);

            texto_total_itbis.setText("" + impuestos.doubleValue());
            texto_total.setText("" + total.doubleValue());
            texto_total_totales.setText("" + totalPago.doubleValue());


        }

        boton_borrar_detalle.setEnabled(true);
        boton_limpiar_detalle.setEnabled(true);
        // boton_guardar_pedido.setEnabled(true);
        boton_facturar_pedido.setEnabled(true);

    }
}
